<template>
    <div class="madp-box">
        <div class="madp-left-box">
            <div class="madp-customer-list">客户列表({{ personList.length }})</div>
            <el-input placeholder="搜索客户" class="madp-input" prefixi-icon="el-con-search" clearable v-model.trim="inputValue" @change="searchFn" maxlength="20"></el-input>
            <div :class="activeIndex == index ? 'madp-center-active madp-center-contact' : 'madp-center-contact'" v-for="(item, index) in personList" :key="index" @click="activeClick(index, item.id)">
                <div class="madp-center-img">
                    <img class="madp-picture" :src="item.avatar" :onerror="`src='/img/male.png'`" />
                    <span>{{ item.name }}</span>
                </div>
                <div class="madp-center-icon">
                    <span class="madp-wechat">@微信</span>
                    <img class="madp-icon" :src="item.gender == 2 ? '/img/female.png' : '/img/male.png'" alt />
                </div>
            </div>
            <el-pagination
                v-if="page1.total > page1.size"
                class="content-page"
                background
                :current-page="page1.current"
                @size-change="handleSizeChange1"
                @current-change="handleCurrentChange1"
                :page-sizes="[10, 20, 30, 50, 100]"
                :page-size="page1.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page1.total"
            ></el-pagination>
        </div>
        <div class="madp-center-box">
            <div v-if="this.activeIndex == -1">
                <div class="madp-right-empty">
                    <div>
                        <img src="/img/nodata.png" class alt />
                    </div>
                </div>
                <div class="madp-right-empty">
                    <div>请选择客户</div>
                </div>
            </div>
            <div v-else>
                <div class="madp-customer-list" v-if="personList.length > 0">{{ personList[this.activeIndex].name }}</div>
                <el-input placeholder="搜索客户" class="madp-input" prefix-icon="el-icon-search" v-model.trim="input2" @change="searchFn2" clearable maxlength="20"></el-input>
                <el-tabs v-model="activeType" class="madp-tabs-left" @tab-click="changeType" stretch>
                    <el-tab-pane v-for="item in typeList" :key="item.id" :label="item.value" :name="item.id">
                        <div class="madp-center-empty" v-if="dataList.length == 0">
                            <img class="madp-no-link" src="/img/nodata.png" alt />
                            <div>暂无数据</div>
                        </div>
                        <div v-for="(it, idx) in dataList" :key="idx">
                            <div v-if="dataList.length == 0">
                                <span>暂无联系人</span>
                            </div>
                            <div v-else :class="activeIdx == idx ? 'madp-center-list madp-center-active' : 'madp-center-list'" @click="selectClick(idx)">
                                <img class="madp-center-img" :src="it.avatar == '' ? '/img/group.png' : it.avatar" />
                                <div class="madp-center-name">{{ it.name }}</div>
                            </div>
                        </div>
                    </el-tab-pane>
                </el-tabs>
                <el-pagination
                    v-if="page2.total > page2.size"
                    class="content-page"
                    background
                    :current-page="page2.current"
                    @size-change="handleSizeChange2"
                    @current-change="handleCurrentChange2"
                    :page-sizes="[10, 20, 30, 50, 100]"
                    :page-size="page2.size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="page2.total"
                ></el-pagination>
            </div>
        </div>
        <div class="madp-right-box">
            <div v-if="activeIdx == -1">
                <div class="madp-right-empty">
                    <div>
                        <img src="/img/nodata.png" class alt />
                    </div>
                </div>
                <div class="madp-right-empty">
                    <div>请选择联系人</div>
                </div>
            </div>
            <div v-else class="madp-chat-records">
                <div v-if="dataList.length > 0">与{{ dataList[this.activeIdx].name }}的聊天</div>
                <el-tabs v-model="activeTabType" @tab-click="changeTabType">
                    <el-tab-pane v-for="item in fileTypeList" :key="item.type" :label="item.name" :name="item.type">
                        <el-date-picker
                            v-model="value1"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="yyyy-MM-dd"
                            :picker-options="{
                                disabledDate(time) {
                                    return time.getTime() > Date.now();
                                }
                            }"
                            @change="getChatRecordsList"
                        ></el-date-picker>
                        <el-button @click="clearFn" class="madp-clear-btn">清空</el-button>
                        <div class="madp-center-empty" v-if="chatList.length == 0">
                            <img class="madp-no-link" src="/img/nodata.png" alt />
                            <div>暂无数据</div>
                        </div>
                        <div class="madp-chatInfo" v-for="(it, i) in chatList" :key="i">
                            <div>
                                <div>
                                    <p :class="it.isCurrentUser == 0 ? 'madp-chat-link' : 'madp-chat-link madp-chat-right'">
                                        <span v-if="it.isCurrentUser == 0">
                                            <span class="madp-chatInfo-name">{{ it.name }}</span>
                                            <span class="madp-chatInfo-time">{{ it.msgDataTime }}</span>
                                        </span>
                                        <span v-else>
                                            <span class="madp-chatInfo-etime">{{ it.msgDataTime }}</span>
                                            <span class="madp-chatInfo-ename">{{ it.name }}</span>
                                        </span>
                                    </p>
                                    <div v-if="it.type == 1" :class="it.isCurrentUser == 0 ? '' : 'madp-chat-right'">
                                        <p :class="it.isCurrentUser == 0 ? 'madp-chatInfo-chat madp-text-over' : 'madp-chatInfo-echat madp-text-over'">{{ it.content.content }}</p>
                                    </div>
                                    <div v-if="it.type == 2" :class="it.isCurrentUser == 0 ? 'madp-chat-box' : 'madp-chat-box madp-chatInfo-eimg'">
                                        <img
                                            :src="it.content.ossPath"
                                            class="madp-chatInfo-img"
                                            @click="
                                                isLargeImgShow = true;
                                                largeImgUrl = it.content.ossPath;
                                            "
                                        />
                                    </div>
                                    <div v-if="it.type == 13" :class="it.isCurrentUser == 0 ? '' : 'madp-chat-right'">
                                        <p class="madp-chat-link madp-chat-color madp-text-over">
                                            <a :href="it.content.link_url" target="_blank">{{ it.content.link_url }}</a>
                                        </p>
                                    </div>
                                    <div v-if="it.type == 4" :class="it.isCurrentUser == 0 ? '' : 'madp-right-audio'">
                                        <audio :src="it.content.ossPath" controls="controls" class="madp-chatInfo-audio"></audio>
                                    </div>
                                    <div :class="it.isCurrentUser == 0 ? '' : 'madp-chatInfo-eimg'" v-if="it.type == 5">
                                        <video :src="it.content.ossPath" controls="controls" class="madp-chatInfo-img"></video>
                                    </div>
                                    <div v-if="it.type == 7" :class="it.isCurrentUser == 0 ? '' : 'madp-right-audio'">
                                        <!-- 音频 -->
                                        <div v-if="it.content.fileext == 'mp3'">
                                            <audio :src="it.content.ossPath" controls="controls" class="madp-chatInfo-audio"></audio>
                                        </div>
                                        <!-- 视频 -->
                                        <div v-if="it.content.fileext == 'mp4'">
                                            <video :src="it.content.ossPath" controls="controls" class="madp-chatInfo-img"></video>
                                        </div>
                                        <div class="madp-file" v-else>
                                            <div>
                                                <p class="madp-file-name">{{ it.content.filename }}</p>
                                                <p>{{ (it.content.filesize / 1024).toFixed(2) }}kb</p>
                                            </div>
                                            <a :href="it.content.ossPath" target="_blank">
                                                <img src="/img/xlsx.png" v-show="it.content.fileext == 'xlsx'" class="madp-chatInfo-file" />
                                                <img src="/img/pdf.jpg" v-show="it.content.fileext == 'pdf'" class="madp-chatInfo-file" />

                                                <img src="/img/ppt.jpg" v-show="it.content.fileext == 'pptx'" class="madp-chatInfo-file" />

                                                <img src="/img/txt.png" v-show="it.content.fileext == 'txt'" class="madp-chatInfo-file" />

                                                <img src="/img/word.png" v-show="it.content.fileext == 'docx'" class="madp-chatInfo-file" />

                                                <img src="/img/csv.png" v-show="it.content.fileext == 'csv'" class="madp-chatInfo-file" />
                                                <img :src="it.content.ossPath" v-show="it.content.fileext == 'jpg'" class="madp-chatInfo-file" />
                                                <img :src="it.content.ossPath" v-show="it.content.fileext == 'png'" class="madp-chatInfo-file" />
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </div>
        <!-- 点击放大图片  -->
        <large-img
            :visible="isLargeImgShow"
            :imgUrl="largeImgUrl"
            @close="
                isLargeImgShow = false;
                imgUrl = '';
            "
        />
    </div>
</template>
<script>
import { fromusers_con, chatCustomerObject, chatRecords } from '@/api/enterprise/session-archiving';
import LargeImg from '@/components/large-img';
export default {
    components: { LargeImg },
    data() {
        return {
            isLargeImgShow: false,
            largeImgUrl: '',
            value1: '',
            chatList: [],
            fileSize: '', //文件尺寸大小
            activeType: '0',
            activeTabType: '0',
            activeIndex: -1,
            activeIdx: -1,
            input2: '',
            inputValue: '',
            personList: [],
            typeList: [
                {
                    value: '单聊',
                    id: '0'
                },
                {
                    value: '群聊',
                    id: '2'
                }
            ],
            dataList: [],
            fileTypeList: [
                {
                    name: '全部',
                    type: '0'
                },
                {
                    name: '文本',
                    type: '1'
                },
                {
                    name: '图片',
                    type: '2'
                },
                {
                    name: '链接',
                    type: '13'
                },
                {
                    name: '语音',
                    type: '4'
                },
                {
                    name: '视频',
                    type: '5'
                },
                {
                    name: '文件',
                    type: '7'
                }
            ],
            page1: {
                current: 1,
                size: 10,
                total: 0
            },
            page2: {
                current: 1,
                size: 10,
                total: 0
            }
        };
    },
    mounted() {
        this.getCustomerList();
    },
    methods: {
        handleSizeChange1(val) {
            this.page1.size = val;
            this.getCustomerList();
        },
        handleCurrentChange1(val) {
            this.page1.current = val;
            this.getCustomerList();
        },
        getCustomerList() {
            let params = {
                type: 1,
                name: this.inputValue,
                current: this.page1.current,
                size: this.page1.size
            };
            fromusers_con(params).then((res) => {
                if (res.data && res.data.code === 0) {
                    this.personList = res.data.data.records;
                    this.page1.total = res.data.data.total;
                }
            });
        },
        handleSizeChange2(val) {
            this.page2.size = val;
            this.getChatObjectList();
        },
        handleCurrentChange2(val) {
            this.page2.current = val;
            this.getChatObjectList();
        },
        searchFn2() {
            this.page2.current = 1;
            this.getChatObjectList();
        },
        getChatObjectList() {
            let params = {
                name: this.input2,
                usertype: this.activeType,
                userId: this.personList[this.activeIndex].id,
                current: this.page2.current,
                size: this.page2.size
            };
            chatCustomerObject(params).then((res) => {
                if (res.data && res.data.code == 0) {
                    this.dataList = res.data.data.records;
                    this.page2.total = res.data.data.total;
                }
            });
        },
        //会话对象得聊天内容
        getChatRecordsList() {
            let params = {
                toUserId: this.dataList[this.activeIdx].id,
                toUserType: this.activeType,
                type: this.activeTabType, //聊天内容类型
                userId: this.personList[this.activeIndex].id,
                userType: 1, //检索对象类型
                dateTimeStart: this.value1[0],
                dateTimeEnd: this.value1[1]
            };
            chatRecords(params).then((res) => {
                if (res.data && res.data.code == 0) {
                    this.chatList = res.data.data.records;
                }
            });
        },
        searchFn() {
            this.getCustomerList();
        },
        activeClick(val) {
            console.log('val,id---', val);
            this.page2.current = 1;
            this.activeIndex = val;
            this.getChatObjectList();
        },
        selectClick(val) {
            this.activeIdx = val;
            this.getChatRecordsList();
        },
        changeType() {
            this.page2.current = 1;
            this.getCustomerList();
            this.getChatObjectList();
        },
        changeTabType() {
            this.getChatRecordsList();
        },
        clearFn() {
            this.value1 = '';
            this.getChatRecordsList();
        }
    }
};
</script>
<style lang="scss" scoped>
a {
    text-decoration: none;
    color: #fff;
}
.clearfix:after {
    content: '';
    display: block;
    clear: both;
    height: 0;
}
.clearfix {
    display: inline-block;
}
.madp-box {
    display: flex;
    min-height: calc(100vh - 12rem);
}
.madp-left-box {
    width: 25%;
    min-width: 26.1rem;
    border-right: 2px solid #ccc;
}
.madp-customer-list {
    font-size: 18px;
    margin: 15px 0;
}
.madp-center-box {
    width: 25%;
    min-width: 400px;
    border-right: 2px solid #ccc;
    padding: 0 15px;
}
.madp-right-box {
    flex: 1;
}
.madp-input {
    margin-bottom: 15px;
}
.madp-center-active {
    background: #ebf4fc;
}
.madp-center-contact {
    padding: 0.32rem;
    border-bottom: 1px solid #ccc;
    display: flex;
    justify-content: space-between;
    margin-right: 5px;
    .madp-center-img {
        margin-right: 10px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .madp-picture {
            width: 40px;
            height: 40px;
            border-radius: 4px;
            margin-right: 5px;
        }
    }
    .madp-center-icon {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        .madp-wechat {
            color: #4bde03;
        }
        .madp-icon {
            width: 20px;
            height: 22px;
            margin-left: 5px;
        }
    }
}
.madp-right-empty {
    display: flex;
    justify-content: center;
    align-items: center;
}
.madp-center-list {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    padding: 5px;
    margin: 5px;
    border-bottom: 1px solid #eee;
    .madp-center-img {
        width: 40px;
        height: 40px;
        border-radius: 4px;
        margin-right: 10px;
    }
    .madp-center-name {
        font-size: 15px;
    }
}
.madp-chat-records {
    margin: 5px;
    padding: 5px;
}
.madp-chatInfo {
    margin: 25px auto 68px;
    .madp-chatInfo-name {
        color: #696969;
        font-size: 12px;
    }
    .madp-chatInfo-ename {
        color: #696969;
        font-size: 12px;
        padding-left: 20px;
    }
    .madp-chatInfo-etime {
        color: #80a7e6;
        font-size: 12px;
    }
    .madp-chatInfo-time {
        color: #80a7e6;
        font-size: 12px;
        padding-left: 20px;
    }

    .madp-chatInfo-chat {
        display: inline-block;
        padding: 8px 10px;
        border-radius: 8px;
        background: #2a57a0;
        color: #fff;
        font-size: 15px;
        margin-top: 10px;
    }
    .madp-chatInfo-img {
        display: block;
        width: 125px;
        height: 125px;
        padding: 8px;
        border-radius: 8px;
        background: #2a57a0;
        color: #fff;
        font-size: 15px;
    }
    .madp-chatInfo-file {
        width: 84px;
        height: 84px;
        object-fit: contain;
    }
}
/deep/.el-date-editor .el-range-separator {
    width: 7%;
}
.madp-chat-link {
    margin: 10px 0;
}
.madp-chat-right {
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
}
.madp-chatInfo-echat {
    padding: 8px 10px;
    border-radius: 8px;
    background: #2a57a0;
    color: #fff;
    font-size: 15px;
}
.madp-chatInfo-audio {
    padding: 8px;
    height: 34px;
    border-radius: 8px;
    background: #2a57a0;
    color: #fff;
    font-size: 15px;
    margin-top: 10px;
}
.madp-right-audio {
    float: right;
}
.madp-chatInfo-image {
    float: right;
}
.madp-no-link {
    height: 80px;
    margin-top: 10px;
}
.madp-center-empty {
    text-align: center;
}
.madp-chat-box {
    height: 160px;
}
.madp-clear-btn {
    margin-left: 15px;
}
.madp-file {
    height: 104px;
    width: 250px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid #ccc;
    padding: 10px;
    border-radius: 8px;
}
// .madp-chat-color {
//     background: #2a57a0;
//     padding: 8px 10px;
//     height: 34px;
//     border-radius: 8px;
//     color: #fff;
//     font-size: 15px;
// }
.madp-file-name {
    width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.madp-text-over {
    max-width: 650px !important;
}
.madp-chatInfo-eimg {
    display: flex;
    justify-content: flex-end;
    width: 100%;
    height: 125px !important;
    padding: 8px;
    border-radius: 8px;
    color: #fff;
    font-size: 15px;
    margin-top: 10px;
}
.content-page {
    margin-top: 10px;
}
</style>
